<template>
  <el-dialog :visible="visible" @close="close">
    <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
      <el-form-item prop="name" label="角色名称">
        <el-input v-model="roleForm.name" style="width:300px" size="mini" />
      </el-form-item>

      <el-form-item label="启用">
        <el-switch v-model="roleForm.state" :active-value="1" :inactive-value="0" size="mini" />
      </el-form-item>

      <el-form-item prop="description" label="角色描述">
        <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
      </el-form-item>

      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="subMit">确定</el-button>

            <el-button size="mini" @click="close">取消</el-button>

          </el-col>

        </el-row>

      </el-form-item>

    </el-form>
  </el-dialog>

</template>
<script>
import { addRoleApi } from '@/api/role'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async subMit() {
      await this.$refs.roleForm.validate()
      addRoleApi(this.roleForm)
      this.$message.success('添加成功')
      this.$emit('update:visible', false)
      this.$emit('updateList')
    },
    close() {
      this.$emit('update:visible', false)
      this.roleForm = {}
    }
  }
}
</script>
